package com.sunday.aomicaichang.base;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.aomicaichang.constValues.ConstValue;
import com.sunday.aomicaichang.service.LocationService;
import com.sunday.member.SessionToken;
import com.sunday.member.entity.Member;
import com.sunday.member.entity.order.OrderItem;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

/**
 * Created by 刘涛 on 2017/5/22.
 */

public class BaseApp extends Application implements Thread.UncaughtExceptionHandler {
    private static BaseApp instance;

    public synchronized static BaseApp getInstance() {
        return instance;
    }


    private Member member;


    private boolean isLogin;
    public LocationService locationService;

    private OrderItem order;


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        if (getCurProcessName(this) != null && getCurProcessName(this).equals("com.sunday.aomicaichang")) {
            /***
             * 初始化定位sdk，建议在Application中创建
             */
            //SDKInitializer.initialize(getApplicationContext());
            locationService = new LocationService(getApplicationContext());
            Thread.setDefaultUncaughtExceptionHandler(this);
            UMShareAPI.get(getApplicationContext());
            PlatformConfig.setWeixin(ConstValue.APP_ID, ConstValue.APPSecret);
            PlatformConfig.setSinaWeibo(ConstValue.SINA_APPKEY,ConstValue.SINA_APP_SEC,"http://sns.whalecloud.com");
            PlatformConfig.setQQZone(ConstValue.QQ_APPID,ConstValue.QQ_APP_KEY);
            Config.DEBUG = true;
            Config.isJumptoAppStore = true;
            //微信 appid appsecret
        }

        member = (Member) SharePerferenceUtils.getIns(instance).getOAuth();
        if (member != null) {
            BaseApp.getInstance().setMember(member);
            SessionToken.accountId = String.valueOf(member.getId());
            SessionToken.token = member.getToken();
        }
    }

    private String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    public void setOrder(OrderItem order){
        this.order=order;
    }




    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        e.printStackTrace();
        System.exit(0);
    }


    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean login) {
        isLogin = login;
    }


    public OrderItem getOrder() {
        return order;
    }
}
